package com.icinfo.cloud.provider.common.oss.service.impl;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.dto.FileInfoSaveDto;
import com.icinfo.cloud.provider.common.enums.FileTransferFormEnum;
import com.icinfo.cloud.provider.common.oss.service.IOssOperateService;
import com.icinfo.framework.common.web.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 描述：Oss操作服务实现类
 *
 * @author xiangyuyu
 * @date 2022/3/12 12:02
 */
@Service
@Slf4j
public class OssOperateServiceImpl implements IOssOperateService {

    @Value("${document.url}")
    private String commonOssUploadUrl;
    @Value("${document.api.uploadBase64File}")
    private String commonOssUploadUri;

    /**
     * 描述：上传文件
     *
     * @param fileInfo {@link FileInfoSaveDto} 文件信息
     * @return {@link Result <String>} 上传成功的文件oss地址
     * @author xiangyuyu
     * @date 2022-03-12 11:54
     */
    @Override
    public Result<String> uploadFile(FileInfoSaveDto fileInfo) {
        String fileTransferForm = fileInfo.getFileTransferForm();
        Boolean isLegalTransferType = FileTransferFormEnum.isLegalEnumCode(fileTransferForm);
        if (!isLegalTransferType) {
            return Result.error("请指定有效的文件传输形式");
        }
        JSONObject jsonObject = new JSONObject();
        byte[] fileBytes;
        if (FileTransferFormEnum.FILE_TRANSFER_FORM_3.getCode().equals(fileTransferForm)) {
            fileBytes = fileInfo.getFileBytes();
            if (ObjectUtils.isEmpty(fileBytes)) {
                return Result.error("请指定文件对应的字节数组");
            }
            jsonObject.put("fileBytes", fileBytes);
            jsonObject.put("fileType", fileInfo.getFileType());
            jsonObject.put("fileName", fileInfo.getFileName());
        } else {
            return Result.error("目前仅支持 字节数组 上传");
        }
        String result = HttpUtil.post(commonOssUploadUrl +commonOssUploadUri, jsonObject.toString());
        if (ObjectUtils.isEmpty(result)) {
            return Result.error("upload fail(file common system return null)");
        }
        JSONObject resultJsonObject = JSONObject.parseObject(result);
        Object code = resultJsonObject.get(Constant.STRING_CODE_LOWER);

        if (!ObjectUtils.isEmpty(code) && (Constant.INTEGER_200 == (int)code)) {
            Object data = resultJsonObject.get(Constant.STRING_DATA_LOWER);
            if (!ObjectUtils.isEmpty(data)) {
                JSONObject dataJsonObject = (JSONObject) data;
                String fileUrl = dataJsonObject.getString("ossUrl");
                if (ObjectUtils.isEmpty(fileUrl)) {
                    return Result.error("上传成功，但未获取到文件oss地址");
                } else {
                    return Result.success("上传成功", fileUrl);
                }
            } else {
                return Result.error("上传成功，但未获取到上传oss的相关文件信息");
            }
        } else {
            return Result.error("上传失败");
        }
    }
}
